package com.Nahida.NaHome.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.util.Log;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.Nahida.NaHome.R;
import com.Nahida.NaHome.activity.base.BaseActivity;
import com.Nahida.NaHome.adapter.RealAppsAdapter;

import java.util.List;

public class RealAppsActivity extends BaseActivity {

    RecyclerView recyclerView;
    List<ResolveInfo> appInfoList;
    RealAppsAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_realapps);

        recyclerView = findViewById(R.id.recyclerView);
        refresh(true);
    }

    @SuppressLint("NotifyDataSetChanged")
    public void refresh(boolean firstLoad){
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        appInfoList = getPackageManager().queryIntentActivities(intent,0);

        Log.e("debug","refresh");

        if(firstLoad || adapter==null) {
            adapter = new RealAppsAdapter(this, appInfoList);
            recyclerView.setLayoutManager(new GridLayoutManager(this,2, LinearLayoutManager.HORIZONTAL,false));
            recyclerView.setAdapter(adapter);
        }
        else {
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onBackPressed() {
        finish();
    }

    @Override
    protected void onPause() {
        super.onPause();
        finish();
    }
}